/*
 *  Copyright (c) 2010 Stephen Nelson <stephen@sfnelson.org>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

package org.sfnelson.bma.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
/**
 * @author stephen
 *
 */
/**
 * @author stephen
 *
 */
public class Bma implements EntryPoint, AsyncCallback<UserData> {

	private final KeyServiceAsync keyService = GWT.create(KeyService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		RootPanel.get().add(new ErrorConsole());

		keyService.retrieveUserData(this);
	}

	public void onFailure(Throwable caught) {
		ErrorConsole.addMessage("Unable to retrieve user data from server", caught);
	}

	public void onSuccess(UserData result) {
		if (result.serial == null) {
			showWelcome();
		} else {
			showKey(result);
		}
	}

	public void showWelcome() {
	    RootPanel.get().add(new WelcomePanel(this, keyService));
	}

    public void showExisting() {
        RootPanel.get().add(new UseExistingPanel(keyService, this));
    }

    public void showKey(UserData data) {
        RootPanel.get().add(new KeyField(this, data, keyService));
    }
}
